package com.example.dict.data;
import com.example.dict.utils.Dictionary.dictionaryOperations;
import com.example.dict.utils.util.Record;

import java.io.*;

/**
 * 单词加载器
 * @author wey
 * */
public class WordLoader extends Thread{

    private final dictionaryOperations dictionary;

    private final String src;

    public WordLoader(dictionaryOperations dictionary, String src) {
        this.dictionary = dictionary;
        this.src = src;
    }

    @Override
    public void run() {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(src)));
            br.readLine();
            String line;
            while (null != (line = br.readLine())) {
                String[] newRecord = line.split("\\s+");
                synchronized (dictionary) {
                    if(newRecord.length != 0) {
                        String trans = new String("");
                        for (int i = 1; i < newRecord.length; i++) {
                            trans += newRecord[i] + " ";
                        }
                        dictionary.insert(newRecord[0], trans);

                    }
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }



}
